{
  //声明函数                     3            'love'
  function createArray(count: number, value: any) {
    //声明一个数组
    let arr: any[] = [];
    //创建
    for (let i = 0; i < count; i++) {
      arr.push(value);
    }
    //返回数组
    return arr;
  }

  //调用函数
  let arr = createArray(3, "iloveyou");
  // console.log(arr[0].slice(1, 5))
  // console.log(arr[0].toFixed(2));
}

{
  //声明函数         泛型变量, 保存类型, 泛型变量的名字没有特殊限制, 常用的名字有 T P V A B C c
  function createArray<T>(count: number, value: T) {
    //声明一个数组
    let arr: T[] = [];
    //创建
    for (let i = 0; i < count; i++) {
      arr.push(value);
    }
    //返回数组
    return arr;
  }

  //调用函数
  let arr = createArray<string>(3, "iloveyou");
  console.log(arr[0].slice(1, 5));
  // console.log(arr[0].toFixed(2));
}

{
  //声明函数, 创建数组
  function createArray<T, P>(v1: T, v2: P): [T, P] {
    return [v1, v2];
  }

  let res = createArray<string, number>("白色", 18);

  // res[0].toFixed();
  // res[1].toFixed();
}

{
  interface Response<T> {
    status: number;
    statusText: string;
    headers: object;
    data: T;
  }

  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User {
    id: number;
    name: string;
    age: number;
  }

  let res: Response<Book> = {
    status: 200,
    statusText: "OK",
    headers: {
      contentType: "xxx",
    },
    data: {
      id: 1,
      title: "西游记",
      price: 19.0,
    },
  };

  let res2: Response<User> = {
    status: 200,
    statusText: "OK",
    headers: {
      contentType: "xxx",
    },
    data: {
      id: 2,
      name: "zhangsan",
      age: 18,
    },
  };
}

{
  interface Response<T> {
    status: number;
    statusText: string;
    headers: object;
    data: T;
  }

  interface ResponseData<T>{
    code: string;
    msg: string;
    data: T
  }

  interface Book {
    id: number;
    title: string;
    price: number;
  }

  interface User {
    id: number;
    name: string;
    age: number;
  }

  let res: Response<ResponseData<Book>> = {
    status: 200,
    statusText: "OK",
    headers: {
      contentType: "xxx",
    },
    data: {
      code: "0000",
      msg: "获取成功",
      data: {
        id: 1,
        title: "西游记",
        price: 19.0,
      },
    },
  };

  let res2: Response<ResponseData<User>> = {
    status: 200,
    statusText: "OK",
    headers: {
      contentType: "xxx",
    },
    data: {
      code: '0000',
      msg: '获取成功',
      data: {
        id: 2,
        name: "zhangsan",
        age: 18,
      }
    },
  };

  // res.data.data.abc
}

{
  interface Book {
    id: number;
    title: string;
    price: number;
  }
  //泛型类   字符串的数组
  class Container<T>{
    store: T[];

    constructor(arg: T[]){
      this.store = arg;
    }

    add(str: T){
      this.store.push(str);
    }

    del(){
      this.store.pop();
    }
  }

  //实例化
  let c = new Container<number>([1,2,3]);

  let c2 = new Container<Book>([]);

  c2.add({id: 2, title: '三国演义', price: 9.9});

}

{
  //通过 ctrl + 点击的方式可以查看内部的声明
  let arr: Array<string> = ['1','2','3'];

  let p = new Promise<number>((resolve, reject) => {
    resolve(20);
  })
}

{
  interface LengthRequired{
    length: number;
  }

  function main<T extends LengthRequired>(arg: T){
    console.log(arg.length);
  }
  // main(123);
  // main('123');
  main([1,2,3]);
}